package com.eureka.eurekaserviceinvoker.controller;

import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 *
 * Created by earl on 2018/4/17.
 */
@Configuration
@RestController
@RequestMapping("/invoker")
public class InvokerController {


    /*
    使用@LoadBalanced注解的RestTemplate将具有负载均衡功能
    Spring容器启动时，会将这些加上LoadBalanced注解的RestTemplate添加拦截器
    拦截器使用LoadBalancerClient处理请求（MyLoadBalancerController模拟了这个过程）
     */
    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;


    @Autowired
    private SpringClientFactory factory;

    //测试负载均衡
    @GetMapping("/router")
    public String person( ){
        RestTemplate restTemplate = getRestTemplate();
        //通过服务提供者的应用名称(eureka-service-provider项目的yml设置spring.application.name)调用接口
        String uri = "http://eureka-service-provider/provider/person/2?name=demoTest";
        return restTemplate.getForObject( uri, String.class);
    }

    //测试LoadBalancer
    @GetMapping("/testLoadBalancer")
    public ServiceInstance testLoadBalancer(){
        //指定某个服务提供者(yml中的spirng.application.name)
        ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-service-provider");
        return serviceInstance;
    }


    //测试SpringClientFactory
    @GetMapping("/testSpringClientFactory")
    public String defaultValue(){
        System.out.println("---------输出配置------");
        ZoneAwareLoadBalancer alb = (ZoneAwareLoadBalancer) factory.getLoadBalancer("default");
        System.out.println(" IClientConfig : " + factory.getLoadBalancer("default".getClass().getName()));
        System.out.println("IRule : " + alb.getRule().getClass().getName());
        System.out.println("IPing : " + alb.getPing().getClass().getName());
        System.out.println("ServerList:  " + alb.getServerListImpl().getClass().getName());
        System.out.println("ServerListFilter : " + alb.getFilter().getClass().getName());
        System.out.println("ILoadBalancer: " + alb.getClass().getName());
        System.out.println("PingInterval: " + alb.getPingInterval());
        System.out.println("---------输出  eureka-service-provider 配置---------");
        //指定某个服务提供者(yml中的spirng.application.name)
        ZoneAwareLoadBalancer alb2 = (ZoneAwareLoadBalancer) factory
                .getLoadBalancer("eureka-service-provider");
        System.out.println("    IClientConfig: "
                + factory.getLoadBalancer("eureka-service-provider").getClass()
                .getName());
        System.out.println("    IRule: " + alb2.getRule().getClass().getName());
        System.out.println("    IPing: " + alb2.getPing().getClass().getName());
        System.out.println("    ServerList: "
                + alb2.getServerListImpl().getClass().getName());
        System.out.println("    ServerListFilter: "
                + alb2.getFilter().getClass().getName());
        System.out.println("    ILoadBalancer: " + alb2.getClass().getName());
        System.out.println("    PingInterval: " + alb2.getPingInterval());
        return "";

    }





}
